﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;

namespace AgilePlannerDT.Utils
{
    [ValueConversion(/* sourceType */ typeof(DateTime), /* targetType */ typeof(string))]
    public class DateOnlyStringConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(string)) { return null; }

            string date = ((DateTime)value).Date.ToString();

            date = date.Substring(0, date.IndexOf(" "));

            return date;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(DateTime)) { return null; }

            DateTime dt;

            try
            {
                dt = DateTime.Parse((string)value);
            }
            catch (Exception)
            {
                return null;
            }
            return dt;
        }

        #endregion
    }
}
